//
//  HistoryViewController.m
//  Car
//
//  Created by 何晓文 on 15/10/7.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "HistoryViewController.h"
#import "SortTableViewCell.h"
#import "UsedCarTableViewCell.h"
#import "UsedCarTableViewCell.h"
#import "UsedCarModel.h"
#import "CarDetailViewController.h"
#import "CarFindHistory.h"
#import "MyHistory.h"

@interface HistoryViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *newsHistory;
@property (nonatomic, strong) UITableView *carHistory;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *historyArray;
@property (nonatomic, strong)NSMutableArray *carHistoryArr;


@property (nonatomic, assign) int currentIndex;
@end

@implementation HistoryViewController
static NSString * const newsHistory = @"newsHistory";
static NSString * const carHistory = @"carHistory";

- (void)viewDidLoad {
    [super viewDidLoad];
	[self initScrollView];
	[self initTableView];
	[self initSegmentedControl];
	[self initRightButtonItem];
//	self.tabBarController.tabBar.translucent = NO;
	self.navigationController.navigationBar.translucent = NO;
	self.title = @"历史记录";
	//新闻历史
	self.historyArray = [[MyHistory shareHistory] extractDataWithUser:[EnterInformationHandle share].myNumber];
	//找车记录
	    self.carHistoryArr = [[CarFindHistory shareHistory] extractDataWithUser:[EnterInformationHandle share].myNumber];
	
	[_newsHistory registerClass:[SortTableViewCell class] forCellReuseIdentifier:newsHistory];
	
	[_carHistory registerClass:[UsedCarTableViewCell class] forCellReuseIdentifier:carHistory];
	
	_currentIndex = (int)_scrollView.contentOffset.x / kWidth;
}
/**
 *添加右边按钮
 */

- (void)initRightButtonItem {
	UIBarButtonItem *clearHistory = [[UIBarButtonItem alloc]initWithTitle:@"清空历史" style:(UIBarButtonItemStyleDone) target:self action:@selector(clearAllHistory)];
	self.navigationItem.rightBarButtonItem = clearHistory;
}
- (void)clearAllHistory {
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清空所有历史记录" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
	[alert show];

}
#pragma mark <-<-<-<-------AlertViewDelegate------->->->->
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 0) {
		//新闻历史
		[[MyHistory shareHistory] deleteHistoryWithUser:[EnterInformationHandle share].myNumber];
		//找车记录
		[[CarFindHistory shareHistory] deleteCarFindHistoryWithUser:[EnterInformationHandle share].myNumber];
		self.historyArray = nil;
		self.carHistoryArr = nil;
		[self.newsHistory reloadData];
		[self.carHistory reloadData];
	}
						  
}
/**
 *  初始segmentedControl
 */
- (void)initSegmentedControl {
	
	UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"新闻历史", @"找车记录"]];
	[seg addTarget:self action:@selector(changeHistory:) forControlEvents:(UIControlEventValueChanged)];
	seg.momentary = YES;
	self.navigationItem.titleView = seg;
	
}

- (void)changeHistory:(UISegmentedControl *)seg {
		if (seg.selectedSegmentIndex == 0) {
			[_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
		}else if (seg.selectedSegmentIndex == 1){
	[_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
		}
	
}

/**
 *  初始化Scrollview
 */
- (void)initScrollView {
	_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth,kHeight)];
	_scrollView.contentSize = CGSizeMake(kWidth * 2, 0);
	_scrollView.showsHorizontalScrollIndicator = NO;
	_scrollView.bounces = NO;
	_scrollView.pagingEnabled = YES;
	_scrollView.userInteractionEnabled = YES;
	_scrollView.delegate =self;
	[self.view addSubview:_scrollView];
	
}
/**
 *  初始化tableView
 */
- (void)initTableView{
	_newsHistory = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 100)];
	_newsHistory.showsVerticalScrollIndicator = NO;
	_newsHistory.separatorStyle= NO;
	_newsHistory.delegate = self;
	_newsHistory.dataSource = self;
	_newsHistory.tag = 101;
	[self.scrollView addSubview:_newsHistory];
	
	_carHistory = [[UITableView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight - 49)];
	_carHistory.showsVerticalScrollIndicator = NO;
	_carHistory.separatorStyle= NO;
	_carHistory.delegate = self;
	_carHistory.dataSource = self;
	_carHistory.tag = 102;
	[self.scrollView addSubview:_carHistory];

}
#pragma mark <-<-<-<-------tableView Delegate------->->->->
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (tableView.tag == 101) {
		return self.historyArray.count;
	}
	return self.carHistoryArr.count;

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

	return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (tableView.tag == 101) {
		//新闻历史
		SortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsHistory forIndexPath:indexPath];
		RecommendModel *model = self.historyArray[indexPath.row];
		[cell setValueWithModel:model];
		return  cell;
	}
	//找车记录
	UsedCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:carHistory forIndexPath:indexPath];
	UsedCarModel *model = self.carHistoryArr[indexPath.row];
	[cell setWithModel:model];
	return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//新闻历史
	if (tableView.tag == 101) {
		RecommendModel *model = self.historyArray[indexPath.row];
		
		WebViewController *webVC = [[WebViewController alloc] init];
	 webVC.changedID = model.ID;
		[self.navigationController pushViewController:webVC animated:YES];
	}
	//找车记录
	else if (tableView.tag == 102){
		UsedCarModel *model = self.carHistoryArr[indexPath.row];
		
		CarDetailViewController *detail = [[CarDetailViewController alloc] init];
		detail.url = model.ID;
		detail.num = 1;
		[self.navigationController pushViewController:detail animated:YES];
	
	}
}
/**
 *  懒加载
 */
- (NSMutableArray *)historyArray {
	
	if (!_historyArray) {
		_historyArray = [[NSMutableArray alloc] init];
	}
	return _historyArray;
}

- (NSMutableArray *)carHistoryArr {
	if (!_carHistoryArr) {
		_carHistoryArr = [[NSMutableArray alloc] init];
	}
	return _carHistoryArr;
}
@end
